{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b8706a5",
   "metadata": {},
   "source": [
    "* 2022/10/10/week7\n",
    "* python学习记录\n",
    "* 记录人;yunhua"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89653dc2",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## 1.有序的数据结构--字符串（sir）\n",
    "## 2.有序的数据结构--列表（list）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "477a85d8",
   "metadata": {},
   "source": [
    "# 1.字符串（string）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5291b867",
   "metadata": {},
   "source": [
    "1.关于字符串的基本内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77588352",
   "metadata": {},
   "source": [
    "2.关于字符串的方法及函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0d733d4",
   "metadata": {},
   "source": [
    "### 1.1关于字符串的基本内容\n",
    "* 单引号：‘允许包涵有“双”引号’\n",
    "* 双引号：\"允许衔入'单'引号\"\n",
    "* 三重引号：'''三重单引号'''，\"\"\"三重双引导\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf13fead",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'spam eggs'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'spam eggs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "778fb8b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type('spam eggs'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d0bd5e12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type('spam eggs'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f98fa8bd",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (1040986201.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn [4], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    'doesn't\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "'doesn't"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4b4bf6d5",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (4156017026.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn [8], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    \"doesn\"t\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 以上解决方案\n",
    "\"doesn\"t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6a969ba2",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOL while scanning string literal (602564154.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn [7], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    'doesn\\'t\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
     ]
    }
   ],
   "source": [
    "'doesn\\'t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4118e88",
   "metadata": {},
   "source": [
    "### 1.2[关于字符串的方法和函数]\n",
    ">1. 字符串是可以拼接的（字符串\\的运算）\n",
    ">2. 字符串是有顺序的，从第一个字符开始，从0依次往后，有长度，可以用lenO检查字符串长度\n",
    ">3. 字符串的取值\n",
    ">>1.取单个值 ：从左往右（正数，第一个值6），从右往左（负数，第一个值-1）\n",
    ">>2.取多个值 ：切片的方法，两个值做取值的区值\n",
    ">4. 字符串的方法及函数\n",
    ">>1. in查询成员是否存在\n",
    ">>2. str.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "637a5bab",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "new_name = [\"小明\",\"小红\",\"张杰\",\"李华\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "564f04c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州南方学院团委欢迎小明\n",
      "广州南方学院团委欢迎小红\n",
      "广州南方学院团委欢迎张杰\n",
      "广州南方学院团委欢迎李华\n"
     ]
    }
   ],
   "source": [
    "for i in new_name:\n",
    "    print(\"广州南方学院团委欢迎\" +i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2f732234",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委欢迎小明'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州南方学院团委欢迎\"+\"小明\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6ec85396",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委欢迎小明'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = '广州南方学院团委欢迎小明'\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dc0fe0d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3a20287f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e5597b15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'南'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7b54a1fc",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn [21], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m text[\u001b[38;5;241m12\u001b[39m]\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "text[12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d1bf401e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1def19e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[6]+text[7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "aee2aaa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 切片 的方法进行多个取值\n",
    "text[6:8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fb4447b",
   "metadata": {},
   "source": [
    "* in查询成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8f83f177",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'广州' in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "53fa9a8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'北京' in text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a969d851",
   "metadata": {},
   "source": [
    "*str.count()查询数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "05504e4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.count('广州')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06ce8c2a",
   "metadata": {},
   "source": [
    "* str.isdigit()判断字符串是否是数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4ddbe8b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "print(19)\n",
    "print('19')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "3c0495bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "age_li = 19\n",
    "age_vang = '19'\n",
    "print(age_li,type(age_li))\n",
    "print(age_wang,type(age_wang))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "e5337671",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_vang.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "b4e65870",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.isdigit()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40aec04c",
   "metadata": {},
   "source": [
    "# 2.有序的数据结构---列表（list）\n",
    ">1. 列表的基本运算法则（参考，有序的数据结构的运算法则列表）\n",
    ">2. 列表的一般方法（要求：全部掌握，并且作总结在课后笔记）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3abf2e65",
   "metadata": {},
   "source": [
    "## 2.1列表的基本运算法则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6d9ebc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：假设，有这样一个场景，我要录入/存放一个班的同学信息（名单信息）。用字符串存放是否可行？如何取值呢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a7f41946",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串存放\n",
    "网二_name ='李华,小明,李晓华.小红'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f5c63fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(网二)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f099b073",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串取值 取值较为麻烦\n",
    "网二_name[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "49b7c8e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小明'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[3:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f300354e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓华'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[6:9]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f437f461",
   "metadata": {},
   "source": [
    "* 对比字符串和列表\n",
    "> 1. 字符串是按照字符进行数据取值\n",
    "> 2. 列表是按照元素进行数据取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "96e08f06",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list = [\"李华\",\"小明\",\"李晓华\",\"小红\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9d5d2829",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0e2b7cbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "26bd736c",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_age_list = [19,18,19,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "129130f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_age_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "18d55900",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[19, 18, 19, 20]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_age_list[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b25f3517",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_age_list.count(19)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c00d7934",
   "metadata": {},
   "source": [
    "# 2.2列表的方法\n",
    ">1. 增加\n",
    ">2. 删除\n",
    ">3. 修改\n",
    ">4. 查询in，count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c154dab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Marry\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc2a8a0b",
   "metadata": {},
   "source": [
    "* 学习方法\n",
    ">1. listname.Tab键查看所有方法名称\n",
    ">2. help(listname.function)\n",
    "\n",
    "> 课后练习(做一个表格，总结列表的方法)："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7f3c54a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "f2d30a10",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"yunhua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "755d3fb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Marry', 'Jan', 'Jack', 'yunhua']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "39bfdd3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
